﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using Acms.Bll;

namespace Acms.DataControls
{
    public class AcmsFlatSiteMapDataSource : System.Web.UI.DataSourceControl
    {
        public AcmsFlatSiteMapDataSource()
        {
            SiteVersion = DateTime.Now;
        }

        private AcmsFlatSiteMapDataSourceView dataView;
        protected override System.Web.UI.DataSourceView GetView(string viewName)
        {
            if (dataView == null)
            {
                dataView = new AcmsFlatSiteMapDataSourceView(this);
            }
            return dataView;
        }

        public string ParentPath { get; set; }
        public string MenuName { get; set; }
        public int SiteId { get; set; }
        public DateTime? SiteVersion { get; set; }

        internal System.Collections.IEnumerable ExecuteSelect(DataSourceSelectArguments arguments)
        {
            if (string.IsNullOrEmpty(MenuName))
            {
                return Acms.Bll.Post.GetAllBySiteIdAndParentPath(SiteId, ParentPath, SiteVersion);
            }
            else
            {
                var menu = Acms.Bll.Menu.GetByNameAndSiteId(MenuName, SiteId);
                if (menu != null)
                {
                    return Acms.Bll.Post.GetAllBySiteIdMenuIdAndParentPath(SiteId, menu.Id, ParentPath, SiteVersion);
                }
                else
                {
                    return null;
                }
            }
        }

        protected override object SaveViewState()
        {
            ViewState["SiteId"] = SiteId;
            ViewState["ParentPath"] = ParentPath;
            ViewState["SiteVersion"] = SiteVersion;
            return base.SaveViewState();
        }

        protected override void LoadViewState(object savedState)
        {
            base.LoadViewState(savedState);
            if (ViewState["SiteId"] is int)
                SiteId = (int)ViewState["SiteId"];
            ParentPath = ViewState["ParentPath"] as string;
            SiteVersion = ViewState["SiteVersion"]as DateTime?;

        }
    }

    public class AcmsFlatSiteMapDataSourceView : DataSourceView
    {
        public AcmsFlatSiteMapDataSourceView(AcmsFlatSiteMapDataSource owner)
            : base(owner, "")
        {
            this.owner = owner;
        }

        AcmsFlatSiteMapDataSource owner;

        
        protected override System.Collections.IEnumerable ExecuteSelect(System.Web.UI.DataSourceSelectArguments arguments)
        {
            return owner.ExecuteSelect(arguments);
        }

        public override bool CanPage
        {
            get
            {
                return false;
            }
        }

        public override bool CanRetrieveTotalRowCount
        {
            get
            {
                return false;
            }
        }

        public override bool CanDelete
        {
            get
            {
                return false;
            }
        }

        public override bool CanInsert
        {
            get
            {
                return false;
            }
        }

        public override bool CanUpdate
        {
            get
            {
                return false;
            }
        }
    }
}
